package me.kany.project.contact;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;

/**
 * Created by Jason on 16/8/2.
 */
public class BlueMoonContact {

	private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

	public static void main(String[] args) {
		FileWriter fw = null;// 把VCard数据（字符）写入文件
		try {
			fw = new FileWriter(new File("./蓝月亮IT通讯录.vcf"));

			InputStream is = new FileInputStream(
					"/Users/Jason/Documents/IdeaProjects/kany/kany-project-contact/test.xlsx");
			// 同时支持Excel 2003、2007
			Workbook workbook = WorkbookFactory.create(is); // 这种方式 Excel
															// 2003/2007/2010/2013/2016
															// 都是可以处理的
			Sheet sheet = workbook.getSheetAt(0);
			int rowCount = sheet.getPhysicalNumberOfRows(); // 获取总行数
			// 遍历每一行

			for (int r = 3; r < rowCount; r++) {
				Row row = sheet.getRow(r);
//				Map<String, String> map = new HashMap<String, String>();
//				int cellCount = row.getPhysicalNumberOfCells(); // 获取总列数
				// 遍历每一列

				String str = "BEGIN:VCARD\n" + "VERSION:3.0\n" + "ORG;CHARSET=UTF-8:蓝月亮（中国）有限公司\n";

				for (int c = 6; c < 10; c++) {
					Cell cell = row.getCell(c);
					String cellValue = null;
					try {
						int cellType = cell.getCellType();
						switch (cellType) {
						case Cell.CELL_TYPE_STRING: // 文本
							cellValue = cell.getStringCellValue();
							break;
						case Cell.CELL_TYPE_NUMERIC: // 数字、日期
							if (DateUtil.isCellDateFormatted(cell)) {
								cellValue = simpleDateFormat.format(cell.getDateCellValue()); // 日期型
							} else {
								cell.setCellType(1);
								cellValue = cell.getStringCellValue(); // 数字
							}
							break;
						case Cell.CELL_TYPE_BOOLEAN: // 布尔型
							cellValue = String.valueOf(cell.getBooleanCellValue());
							break;
						case Cell.CELL_TYPE_BLANK: // 空白
							cellValue = cell.getStringCellValue();
							break;
						case Cell.CELL_TYPE_ERROR: // 错误
							cellValue = "错误";
							break;
						case Cell.CELL_TYPE_FORMULA: // 公式
							cellValue = "错误";
							break;
						default:
							cellValue = "错误";
						}
					} catch (Exception e) {
						cellValue = "错误";
					}
					// BEGIN:VCARD
					// VERSION:3.0
					// FN;CHARSET=UTF-8:王秋平
					// N;LANGUAGE=zh-cn;CHARSET=UTF-8:王;秋平;;;
					// TEL;CELL;VOICE;CHARSET=UTF-8:15121129503
					// TEL;CELL;VOICE;CHARSET=UTF-8:13316949511
					// TEL;CELL;VOICE;CHARSET=UTF-8:18820203256
					// EMAIL;INTERNET;WORK;CHARSET=UTF-8:Judy.wang@hooman.cn
					// X-MS-ANNIVERSARY:2013-07-16
					// ORG;CHARSET=UTF-8:深圳火人网络科技有限公司;
					// BDAY:1991-08-12
					// TITLE;CHARSET=UTF-8:商务专员
					// NICKNAME;CHARSET=UTF-8:Judy
					// END:VCARD
					switch (c) {
					case 6:
						// 设置名片名字（FNType）。Sets the FN type. This type must be
						// set in the VCard and cannot be omitted, it can
						// however be left with all empty
						// values.文档说这个值必须设置，不能忽略。

						String name = cellValue;
						str += "FN:蓝月亮" + name + "\n";
						str += "N:蓝月亮;" + name + ";;;\n";
						// str += "FN;CHARSET=UTF-8:"+name+"\n";
						// str +=
						// "N;LANGUAGE=zh-cn;CHARSET=UTF-8:"+name.substring(0,1)+";"+name.substring(1)+";;;\n";

						break;
					case 7:
						str += "TITLE;CHARSET=UTF-8:" + cellValue + "\n";
						break;
					case 8:
						str += "TEL;CELL;VOICE;CHARSET=UTF-8:" + cellValue + "\n";
						break;
					case 9:
						String email = cellValue;
						Pattern pattern = Pattern.compile("(.*<)([a-z0-9\\.\\@]+)(.*)");
						Matcher matcher = pattern.matcher(email);
						if (matcher.matches()) {
							email = matcher.group(2);
						}
						str += "EMAIL;INTERNET;WORK;CHARSET=UTF-8:" + email + "\n";
						break;
					default:
						break;
					}
				}
				str += "END:VCARD\n";
				fw.append(str);// 写入文件
				fw.flush();
			}
			fw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
